<?php
require_once __DIR__ . '/cnear.php';
require_once __DIR__ . '/cnext.php';
require_once __DIR__ . '/station.php';
require_once __DIR__ . '/../database/db_connect.php';


class Data
{
	public $dataStations;
	public $dataNextStation;
	public $dataNearStation;
	function __construct()
	{
		$db = new DB_CONNECT();

		$this->dataStations = $this->getAllStation();

		$this->dataNextStation = $this->getAllNext();

		$this->dataNearStation = $this->getAllNearby();
	}
	// ===================================================================================
	function getAllStation() // OK
	{
		// code here
		//$db = new DB_CONNECT();
		$allStations = array();
		$str_query_getAll = "SELECT * FROM stations";
		mysql_query("SET NAMES 'UTF8'");
		$data_allStation = mysql_query($str_query_getAll) 
										 or die(mysql_error());
		while($row = mysql_fetch_array($data_allStation))
		{
			$cAllS = new station();
			$cAllS->lat = $row['lat'];
			$cAllS->lon = $row['lon'];
			$cAllS->name = $row['name'];
			$allStations[$row['id']] = $cAllS;
		}
		return $allStations;
	}
	// ===================================================================================
	function getAllNext()
	{
		// code here
		//$db = new DB_CONNECT();
		$MDNext = array();
		$str_query_getAll = "SELECT * FROM connections";
		$data_allNext = mysql_query($str_query_getAll) 
								  or die(mysql_error());
		while($row = mysql_fetch_array($data_allNext))
		{
			$nextS = array();
			$cNext = new cnext();
			$cNext->next = $row['next'];
			$cNext->route = $row['route'];
			$cNext->distance = $row['distance'];
			if(!array_key_exists($row['station'], $MDNext))
			{
				array_push($nextS, $cNext);
				$MDNext[$row['station']] = $nextS;
			}
			else
			{
				$nextS = $MDNext[$row['station']];
				array_push($nextS, $cNext);
				$MDNext[$row['station']] = $nextS;
			}
		}
		return $MDNext;
	}
	// ===================================================================================
	function getAllNearby() // OK
	{
		// code here
		//$db = new DB_CONNECT();
		$MDNear = array();
		$str_query_getAll = "SELECT * FROM nearby_connections";
		$data_allNearby = mysql_query($str_query_getAll) 
										 or die(mysql_error());
		while($row = mysql_fetch_array($data_allNearby))
		{
			$nearS = array();
			$cNear = new cnear();
			$cNear->near = $row['nearby'];
			$cNear->distance = $row['distance'];
			if(!array_key_exists($row['station'], $MDNear))
			{
				array_push($nearS, $cNear);
				$MDNear[$row['station']] = $nearS;
			}
			else
			{
				$nearS = $MDNear[$row['station']];
				array_push($nearS, $cNear);
				$MDNear[$row['station']] = $nearS;
			}
		}
		return $MDNear;
	}
	// ===================================================================================
	function resultData($md)
	{
		switch($md)
		{
			case 'station':
				return $this->dataStations;
			break;
			case 'nextS':
				return $this->dataNextStation;
			break;
			case 'nearS':
				return $this->dataNearStation;
			break;
		}
	}
}
?>